package com.ygqh.baby.service.impl;

import java.util.List;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgProductImages;
import com.ygqh.baby.mapper.YgProductImagesMapper;
import com.ygqh.baby.service.YgProductImagesService;

import org.springframework.util.Assert;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgProductImagesExample;
import com.ygqh.baby.ao.DataStatus;

@Service
public class YgProductImagesServiceImpl implements YgProductImagesService {

	@Autowired
	private YgProductImagesMapper ygProductImagesMapper;

	@Override
	public List<YgProductImages> find() {
		YgProductImagesExample example = new YgProductImagesExample();
		example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
		return ygProductImagesMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgProductImagesExample example = new YgProductImagesExample();
		example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
		return ygProductImagesMapper.countByExample(example);
	}

	@Override
	public YgProductImages findById(Long id) {
		return ygProductImagesMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgProductImages ygProductImages) {
		return ygProductImagesMapper.insertSelective(ygProductImages);
	}

	@Override
	public int update(YgProductImages ygProductImages) {
		return ygProductImagesMapper.updateByPrimaryKeySelective(ygProductImages);
	}

	@Override
	public ResultSet<YgProductImages> search(QueryInfo queryInfo, String q) {
		List<YgProductImages> list = ygProductImagesMapper.selectSuper(queryInfo, q);
		int count = ygProductImagesMapper.countSuper(q);
		ResultSet<YgProductImages> resultSet = new ResultSet<YgProductImages>(count, list);
		return resultSet;
	}

	@Override
	public int saveBatch(List<YgProductImages> ygProductImages) {
		return ygProductImagesMapper.saveBatch(ygProductImages);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		int count = 0;
		for (Long id : ids) {
			YgProductImages ygProductImages = this.findById(id);
			ygProductImages.setStatus(DataStatus.Delete);
			count += ygProductImagesMapper.updateByPrimaryKeySelective(ygProductImages);
		}

		return count;
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgProductImages ygProductImages) {
		int r = 0;
		if (ygProductImages.getId() != null) {
			r = this.update(ygProductImages);
		} else {
			r = this.save(ygProductImages);
		}
		return r;
	}

	@Override
	public List<YgProductImages> find(Long productId) {
		YgProductImagesExample example = new YgProductImagesExample();
		example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name()).andProductIdEqualTo(productId);
		return ygProductImagesMapper.selectByExample(example);
	}

	@Override
	public int deleteSoftByProductId(Long id) {

		YgProductImagesExample example = new YgProductImagesExample();
		example.createCriteria().andProductIdEqualTo(id);

		YgProductImages img = new YgProductImages();
		img.setStatus(DataStatus.Delete);
		int r = ygProductImagesMapper.updateByExampleSelective(img, example);
		return r;
	}

	@Override
	public YgProductImages findMainImgByProductId(Long productId) {
		YgProductImagesExample example = new YgProductImagesExample();
		example.createCriteria().andProductIdEqualTo(productId).andIsMainEqualTo(true).andStatusEqualTo(DataStatus.Valid.name());
		List<YgProductImages> list = ygProductImagesMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

}
